package com.xyhuo.leetcode;

import java.util.ArrayList;
import java.util.List;

public class BrowserHistory {

    List<String> list = new ArrayList<>();

    int index = 0;

    public BrowserHistory(String homepage) {
        list.add(homepage);
    }

    public void visit(String url) {
        if (index != list.size() - 1) {
            list = list.subList(0, index + 1);
        }
        list.add(url);
        index = list.size() - 1;
    }

    public String back(int steps) {
        if (steps >= index) {
            index = 0;
        } else {
            index = index - steps;
        }
        return list.get(index);
    }

    public String forward(int steps) {
        if (index + steps >= list.size()) {
            index = list.size() - 1;
        } else {
            index = index + steps;
        }
        return list.get(index);
    }

    public static void main(String[] args) {
        BrowserHistory b = new BrowserHistory("leetcode");
        b.visit("goole");
        b.visit("facebook");
        b.visit("youtobe");
        String back = b.back(1);
        String back1 = b.back(1);
        String forward = b.forward(1);
        b.visit("linked");
        String forward1 = b.forward(2);
        String back2 = b.back(2);
        String back3 = b.back(7);
    }
}
